package com.wondersgroup.fjzlpt.org.dao.impl.jtqy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.quick.framework.util.FormatDate;
import com.quick.framework.util.LoggerUtil;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.QysjbDao;
import com.wondersgroup.fjzlpt.org.util.ControllerCommonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 签约事件表--实现层
 * Created by GZB on 2017/9/29.
 */
@Repository
public class QysjbDaoImpl extends BaseJTDAOImpl implements QysjbDao{

    /** 协议号对应日期 */
    private static String XYHDYRQ = null;
    /** 协议号 */
    private static int XYH = 0;
    /* redis */
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    @Autowired
    @Qualifier("TB_JTQY_QYSJB")
    public void setTb(TableBean tb) {
        super.setTb(tb);
    }

    /**
     * 分页查询签约事件
     * @param map
     * @return
     */
    public JSONObject queryPage(Map<String,String> map){
        ControllerCommonUtil.pageFormat(map);
        JSONObject sqlObj = this.getPageSql(map);
        List sqlParams = (List)sqlObj.get("sqlParams");

        PageQuerySQLBean pageBean = super.checkAndCreatePageQuerySQLBean(map, sqlObj.getString("sql"), null, sqlParams.toArray());
        JSONObject result = super.queryPage(pageBean, ResultHandlerConstant.JSONARRAY);
        return result;
    }

    @Override
    public JSONObject queryQyxyId(Map<String, String> map) {
        JSONObject sqlObj = getPageSql(map);
        List sqlParams = sqlObj.getObject("sqlParams" , List.class);
        SQLBean sqlBean = new SQLBean(sqlObj.getString("sql"));
        sqlBean.setParamAry(sqlParams);
        return this.queryOne(sqlBean);
    }

    /**
     * 合成分页sql
     * @param map
     * @return
     */
    private JSONObject getPageSql(Map<String,String> map){
        JSONObject result = new JSONObject();
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select * from ").append(this.tb.getTableName()).append(" a ");
        sqlBuf.append("where 1=1 ");

        List<Object> sqlParams = new ArrayList<Object>();
        String value = map.get("QYXXID");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.QYXXID = ? ");
            sqlParams.add(value);
        }
        value = map.get("SJLX");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.SJLX = ? ");
            sqlParams.add(value);
        }
        sqlBuf.append("order by a.CZSJ desc ");
        result.put("sql",sqlBuf.toString());
        result.put("sqlParams",sqlParams);

        return result;
    }

    /**
     * 按日期查询现在最大的协议号
     * @param JGXZQHDM 机构行政区划代码
     * @return
     */
    public String queryMaxXyh(String JGXZQHDM){
        /** 协议号设定 //协议号生成规则： 机构行政区划代码+ 8位日期 + 5位自增数:  330602 20171019 00001 */
        Date today = FormatDate.getDateY_M_D(FormatDate.getDateYYYY_MM_DD(new Date()));
        XYHDYRQ = FormatDate.getDateYYYYMMDD(today);
        String RedisXYH = redisTemplate.opsForValue().get(XYHDYRQ);
        System.out.println(RedisXYH);
        JSONObject result = new JSONObject();
        if(RedisXYH == null){
            System.out.println(XYHDYRQ);
            StringBuffer sqlBuf = new StringBuffer();
            sqlBuf.append("select MAX(XYH) XYH from ").append(this.tb.getTableName()).append(" a ");
            sqlBuf.append("where to_char(a.CZSJ,'yyyyMMdd') = ? and XYH is not null ");
            sqlBuf.append("order by a.XYH  desc");
            // Date tomorrow = FormatDate.addDay(today,1,"yyyy-MM-dd HH:mm:ss");
            SQLBean bean = new SQLBean(sqlBuf.toString(), XYHDYRQ);
            JSONObject xyhObj = super.queryOne(bean);

            LoggerUtil.info("最大协议号："+xyhObj.getString("XYH"));
            if(xyhObj.isEmpty() || "".equals(xyhObj.getString("XYH"))){
                XYH = 1;
            }else{
                XYH = Integer.parseInt(xyhObj.getString("XYH").substring(8)) + 1;
            }
            redisTemplate.opsForValue().set(XYHDYRQ,String.valueOf(XYH));
        }else{
            Long XYHd = redisTemplate.opsForValue().increment(XYHDYRQ,1);
            XYH = XYHd.intValue();
            LoggerUtil.info("XYH最大协议号："+XYH);
        }
        LoggerUtil.info("协议号增加后："+XYH);
        System.out.println(XYH);
        //5位自增数
        String[] addZeroArr = new String[]{"","0000","000","00","0",""};//加零数组--用于补0

        //4位自增数
        //String[] addZeroArr = new String[]{"","000","00","0","",""};//加零数组--用于补0

        //String XYH_ = JGXZQHDM + XYHDYRQ + addZeroArr[String.valueOf(XYH).length()] + XYH;
        //月份日期 + 4位自增数
        String XYH_ = XYHDYRQ + addZeroArr[String.valueOf(XYH).length()] + XYH;
        System.out.println(XYH_);
        LoggerUtil.info("最终协议号："+XYH_);
        return XYH_;
    }

    /**
     * 根据QYXXID 查询最近的一条协议信息
     * @param map
     * @return
     */
    public JSONObject queryLast(Map<String,String> map){
        JSONObject result = new JSONObject();
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select * from ").append(this.tb.getTableName()).append(" a ");
        sqlBuf.append("where a.QYXXID = ? ");

        List<Object> sqlParams = new ArrayList<Object>();
        sqlParams.add(map.get("QYXXID"));
        String value = map.get("SJLX");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.SJLX = ? ");
            sqlParams.add(value);
        }
        sqlBuf.append("order by a.CZSJ ");
        SQLBean bean = new SQLBean(sqlBuf.toString(), sqlParams.toArray());
        return queryOne(bean);
    }

    /**
     * 查询没有家签协议的记录
     * @param map
     * @return
     */
    public JSONArray queryNotXyRecord(Map map){
        /**
         *  XYID字段等于“XYID”的记录是目标数据
         */
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select a.ID,b.FWBID,b.XM,b.XB,b.ZJHM,b.SJHM,b.JTZZ,b.XZQHDM,b.XZQHMC JGXZQHMC, '是' SFCB, 'addQy' CZXXDIV ,b.FWBMC, b.YLJGMC YFYLJGMC,b.QYYS YFXM,b.YSSJHM YFSJHM, to_char(b.SXSJ , 'yyyy-MM-dd') SXSJ , to_char(b.JSSJ , 'yyyy-MM-dd') JSSJ ");
        sqlBuf.append("from (");
        sqlBuf.append("select ID,QYXXID from ").append(this.tb.getTableName()).append(" where XYID='XYID' and  ROWNUM <= ? ) a ");
        sqlBuf.append("left join V_JTQY_QYXX b on a.QYXXID=b.QYXXID ");

        SQLBean bean = new SQLBean(sqlBuf.toString(), map.get("pageSize"));


        return super.queryList(bean);
    }

    /**
     * 根据条件判断是否存在记录
     * @param map
     * @return
     */
    public boolean isExist(Map<String,String> map){
        StringBuffer sqlBuf = new StringBuffer();
        List sqlParams = new ArrayList();
        sqlBuf.append("select count(ID) from ").append(this.tb.getTableName()).append(" a where 1=1 ");
        String value = map.get("XYH");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.XYH = ? ");
            sqlParams.add(value);
        }
        SQLBean bean = new SQLBean(sqlBuf.toString(), sqlParams.toArray());
        return super.isExist(bean);
    }

    /**
     * 根据条件判断是否存在记录
     * @param XYID
     * @return
     */
    public String queryXyhByXyid(String XYID){
        StringBuffer sqlBuf = new StringBuffer();
        List sqlParams = new ArrayList();
        sqlBuf.append("select XYH from ").append(this.tb.getTableName()).append(" a where XYID = ? ");
        SQLBean bean = new SQLBean(sqlBuf.toString(), XYID);
        JSONObject xyidObj = super.queryOne(bean);
        if(xyidObj.isEmpty()){
            return null;
        }else{
            return xyidObj.getString("XYH");
        }
    }

}
